package com.swust.reactredis.pubsub;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

/**
 * @author: Chen Yixing
 * @since: 2024/5/2 09:18:13
 * @description:
 */
@Component
public class PubSubApplicationRunner implements ApplicationRunner {
    @Autowired
    private PubSubUsage<String> pubSubUsage;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        String topic = "joke";
        String message = "fuck you, joke!";

        pubSubUsage.listenTo(topic).subscribe(s -> System.out.println(s));

        new Thread(() -> {
            while (true) {
                //如果不在线程里慢慢的发送，直接提取到外面，redis一下子订阅不到数据，来不及，所以一般都是启动以后再来消费
                pubSubUsage.publish(topic, Mono.just(message)).subscribe();

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {

                }
            }
        }).start();
    }
}
